package com.site.service.impl.dining;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Service;

import com.site.dao.dining.DishesDao;
import com.site.dto.dining.DishesBizBean;
import com.site.model.dining.DishesInfo;
import com.site.model.dining.RestaurantInfo;
import com.site.service.dining.DishesService;
import com.site.util.PropertyUtils;
import com.site.util.base.businessbean.FileBean;
import com.site.util.common.fileupload.FileUploadHandle;
import com.site.util.exception.ApplicationException;
import com.site.util.exception.SystemException;
import com.site.util.extremetable.QueryPage;

/**
 * 普通菜单Service
 * @author win7
 *
 */
@Service
public class DishesServiceImpl implements DishesService {
	
	private Log log=LogFactory.getLog(this.getClass());

	private DishesDao dishesDao;
	
	@Resource
	public void setDishesDao(DishesDao dishesDao) {
		this.dishesDao = dishesDao;
	}

	@Override
	public Object execute(Object obj, String command) throws SystemException,	ApplicationException {
		// 删除
		if(COMMAND_DELETE.equals(command)){
			delete((Map<String, Object>)obj);
		// 增加
		} else if(COMMAND_ADD.equals(command)){
			add((Map<String, Object>)obj);
		// 修改
		} else if(COMMAND_UPDATE.equals(command)){
			edit((Map<String, Object>)obj);
		}
		
		return null;
	}

	/**
	 * 修改
	 * @param obj
	 */
	private void edit(Map<String, Object> map) {
		if(null != map && !map.isEmpty()){
			DishesBizBean dishesBizBean = (DishesBizBean)map.get("dishesBizBean");
			DishesInfo dishesInfo = dishesDao.getEntityById(DishesInfo.class, dishesBizBean.getId());
			// 旧图片地址
			String oldPath = dishesInfo.getImagePath();
			FileUploadHandle fileUploadHandle = (FileUploadHandle)map.get("fileUploadHandle");
			if(null != fileUploadHandle && fileUploadHandle.getFiles() != null && !fileUploadHandle.getFiles().isEmpty()){
				// 如果有就图片，就删除相应的图片
				if(null != dishesInfo && !StringUtils.isEmpty(dishesInfo.getImagePath())){
					String path = fileUploadHandle.getRequest().getSession().getServletContext().getRealPath("/restaurant");
					File file = new File(path+dishesInfo.getImagePath());
					if(null != file && file.exists()){
						file.delete();
						log.info("餐厅菜单  "+dishesInfo.getImagePath()+" 正在删除…");
					}
				}
				
				// 上传路径[菜品图]目录
				String rno="";
				if(null != oldPath && !StringUtils.isEmpty(oldPath)){
					rno = oldPath.substring(1,7);
				} else {
					RestaurantInfo restaurantInfo = dishesDao.getEntityById(RestaurantInfo.class, dishesBizBean.getRid());
					if(null != restaurantInfo && !StringUtils.isEmpty(restaurantInfo.getRno())){
						rno = restaurantInfo.getRno();
					}
				}
				String mainPath = "/restaurant/"+rno+"/dishes";
				fileUploadHandle.setUploadDir(RestaurantServiceImpl.getUploadPath(fileUploadHandle.getRequest(), mainPath));
				List<FileBean> fileBeans = fileUploadHandle.handleUploadFile();
				
				if(null != fileBeans && !fileBeans.isEmpty()){
					for (FileBean fileBean : fileBeans) {
						if(null != fileBean && !StringUtils.isEmpty(fileBean.getNewName())){
							dishesBizBean.setImagePath("/"+rno+"/dishes/"+fileBean.getNewName());
							log.info("餐厅菜品图  "+fileBean.getNewName()+" 正在上传…");
						}
					}
				}
			}
			PropertyUtils.copyNotNullProperties(dishesInfo, dishesBizBean);
			dishesDao.update(dishesInfo);
		}
	}

	/**
	 * 删除
	 * @param obj
	 */
	private void delete(Map<String, Object> map) {
		if(null != map && !map.isEmpty()){
			DishesBizBean dishesBizBean = (DishesBizBean)map.get("dishesBizBean");
			DishesInfo dishesInfo = dishesDao.getEntityById(DishesInfo.class, dishesBizBean.getId());
			// 删除相应的图片
			if(null != dishesInfo){
				HttpServletRequest request = (HttpServletRequest)map.get("request");
				String path = request.getSession().getServletContext().getRealPath("/restaurant");
				File file = new File(path+dishesInfo.getImagePath());
				if(null != file && file.exists()){
					file.delete();
				}
				log.info("餐厅菜单  "+dishesInfo.getImagePath()+" 正在删除…");
				dishesDao.delete(DishesInfo.class, dishesBizBean.getId());
			}
		}
	}
	
	/**
	 * 增加
	 * @param map
	 */
	private void add(Map<String, Object> map){
		if(map != null && !map.isEmpty()){
			DishesBizBean dishesBizBean = (DishesBizBean)map.get("dishesBizBean");
			RestaurantInfo restaurantInfo = dishesDao.getEntityById(RestaurantInfo.class, dishesBizBean.getRid());
			DishesInfo dishesInfo = new DishesInfo();
			PropertyUtils.copyProperties(dishesInfo, dishesBizBean);
			dishesInfo.setDno(dishesDao.getMaxId("D", 4, DishesInfo.class));
			dishesDao.save(dishesInfo);
			// 增加菜的图片
			FileUploadHandle fileUploadHandle = (FileUploadHandle)map.get("fileUploadHandle");
			if(null != fileUploadHandle && fileUploadHandle.getFiles() != null && !fileUploadHandle.getFiles().isEmpty()){
				// 上传路径[菜品图]目录
				String mainPath = "/restaurant/"+restaurantInfo.getRno()+"/dishes";
				fileUploadHandle.setUploadDir(RestaurantServiceImpl.getUploadPath(fileUploadHandle.getRequest(), mainPath));
				List<FileBean> fileBeans = fileUploadHandle.handleUploadFile();
				
				if(null != fileBeans && !fileBeans.isEmpty()){
					for (FileBean fileBean : fileBeans) {
						if(null != fileBean && !StringUtils.isEmpty(fileBean.getNewName())){
							dishesInfo.setImagePath("/"+restaurantInfo.getRno()+"/dishes/"+fileBean.getNewName());
							log.info("餐厅菜品图  "+fileBean.getNewName()+" 正在上传…");
						}
					}
				}
			}
		}
	}

	@Override
	public List<DishesBizBean> getDishes(QueryPage queryPage, DishesBizBean dishesBizBean) {
		List<DishesInfo> dishesInfos = dishesDao.getDishes(queryPage, dishesBizBean);
		List<DishesBizBean> dishesBizBeans = new ArrayList<DishesBizBean>();
		if(null != dishesInfos && !dishesInfos.isEmpty()){
			for (DishesInfo dishesInfo : dishesInfos) {
				if(dishesInfo != null){
					DishesBizBean dishesBizBean2 = new DishesBizBean();
					PropertyUtils.copyProperties(dishesBizBean2, dishesInfo);
					dishesBizBeans.add(dishesBizBean2);
				}
			}
		}
		if(!dishesBizBeans.isEmpty()){
			return dishesBizBeans;
		}
		return null;
	}

	@Override
	public DishesBizBean getEntityById(Long id) {
		DishesInfo dishesInfo = dishesDao.getEntityById(DishesInfo.class, id);
		if(null != dishesInfo){
			DishesBizBean dishesBizBean = new DishesBizBean();
			PropertyUtils.copyProperties(dishesBizBean, dishesInfo);
			return dishesBizBean;
		}
		return null;
	}
}
